package com.ruowei.modules.foodOrder.web.rest;

import com.ruowei.common.error.exception.CommonException;
import com.ruowei.common.querydsl.OptionalBooleanBuilder;
import com.ruowei.modules.foodOrder.domain.CourierChangeLog;
import com.ruowei.modules.foodOrder.domain.MerchantChangeLog;
import com.ruowei.modules.foodOrder.domain.QCourierChangeLog;
import com.ruowei.modules.foodOrder.domain.QrAddress;
import com.ruowei.modules.foodOrder.repository.CourierChangeLogRepository;
import com.ruowei.modules.foodOrder.web.dto.*;
import io.github.jhipster.web.util.PaginationUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api")
@Transactional
@Api(tags = "配送员修改日志")
public class CourierChangeLogResource {

    private final Logger log = LoggerFactory.getLogger(CourierChangeLogResource.class);

    private final CourierChangeLogRepository courierChangeLogRepository;
    private final QCourierChangeLog QCC = QCourierChangeLog.courierChangeLog;

    public CourierChangeLogResource(CourierChangeLogRepository courierChangeLogRepository) {
        this.courierChangeLogRepository = courierChangeLogRepository;
    }

    @GetMapping("/courier-change-log/list")
    @ApiOperation(value = "配送员修改日志列表")
    public ResponseEntity<List<CourierChangeLog>> getCourierChangeList(CourierChangeLogQM qm, Pageable pageable) {
        OptionalBooleanBuilder predicate = new OptionalBooleanBuilder()
            .notEmptyAnd(QCC.updateTime::goe, qm.getUpdateTimeStart())
            .notEmptyAnd(QCC.updateTime::loe, qm.getUpdateTimeEnd());

        Page<CourierChangeLog> page = courierChangeLogRepository.findAll(predicate.build(), pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), page);
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    @GetMapping("/courier-change-log/{id}")
    @ApiOperation(value = "配送员修改日志详情")
    public ResponseEntity<CourierChangeDTO> getCourierChangeDetail(@PathVariable Long id) {
        if (id == null) {
            throw new CommonException("ID不能为空");
        }
        Optional<CourierChangeLog> courierChangeLog = courierChangeLogRepository.findById(id);
        if (!courierChangeLog.isPresent()) {
            throw new CommonException("配送员修改日志详情不存在");
        }
        CourierChangeDTO dto = new CourierChangeDTO();
        courierChangeLog.ifPresent(log -> {
            CourierChangeDetailDTO changeInfo = new CourierChangeDetailDTO();
            changeInfo.setId(log.getId());
            changeInfo.setCourierCode(log.getCourierCode());
            changeInfo.setName(log.getName());
            changeInfo.setSex(log.getSex());
            changeInfo.setPhone(log.getPhone());
            changeInfo.setAge(log.getAge());
            changeInfo.setLoginCode(log.getLoginCode());
            changeInfo.setPassword(log.getPassword());
            dto.setChangeInfo(changeInfo);
            //修改前日志
            CourierChangeDetailDTO changeBefore = new CourierChangeDetailDTO();
            Optional<CourierChangeLog> changeLog = courierChangeLogRepository.getMerchantChange(log.getCourierCode(), log.getUpdateTime());
            if (changeLog.isPresent()) {
                CourierChangeLog detail = changeLog.get();
                changeBefore.setId(log.getId());
                changeBefore.setCourierCode(detail.getCourierCode());
                changeBefore.setName(detail.getName());
                changeBefore.setSex(detail.getSex());
                changeBefore.setPhone(detail.getPhone());
                changeBefore.setAge(detail.getAge());
                changeBefore.setLoginCode(detail.getLoginCode());
                changeBefore.setPassword(detail.getPassword());
                dto.setChangeBefore(changeBefore);
            } else {
                dto.setChangeBefore(changeInfo);
            }
        });
        return ResponseEntity.ok(dto);
    }
}
